package cn.cj.service.impl;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.jsoup.nodes.Document;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import cn.cj.mapper.FindConditionMapper;
import cn.cj.service.FindCondition;
import cn.cj.util.JsoupUtil;
import cn.cj.vo.JobBean;
import cn.cj.vo.PageBean;

@Service
@EnableScheduling
public class FindConditionImpl implements FindCondition {
    JsoupUtil jsoupUtil = new JsoupUtil();
    /*
     * (non-Javadoc)
     *
     * @see cn.cj.service.impl.FindCondition#search(java.lang.String)
     */
    @Resource
    FindConditionMapper findConditionMapper;
    ObjectMapper mapper;

    @Override
    public String search(String string) throws IOException {
        mapper = new ObjectMapper();
        int number = findConditionMapper.getSearchNum(string);
        String message = "数据收集失败!";
        if (number < 1) {
            int i = findConditionMapper.setJobBean(getJob(string));
            if (i > 0) {
                message = "数据收集完成!";
            }

            return mapper.writeValueAsString(message);
        } else {
            message = "数据已存在!";
            return mapper.writeValueAsString(message);
        }

    }

    @Override
    public List<JobBean> getJob(String string) {
        StringBuffer s = new StringBuffer(
                "http://search.51job.com/jobsearch/search_result.php?fromJs=1&jobarea=040000&keyword=");
        s.append(string);
        s.append("&keywordtype=2&lang=c&stype=2&postchannel=0000&fromType=1&confirmdate=9");
        Document doc;
        try {
            doc = jsoupUtil.getDocument(s.toString());
            int num = jsoupUtil.getPag(doc);
            String url = jsoupUtil.getPagURL(doc);
            List<String> urlList = jsoupUtil.ModifyURL(url, num);
            List<JobBean> list = new ArrayList<JobBean>();
            for (int i = 0; i < urlList.size(); i++) {
                Document doc2 = jsoupUtil.getDocument(urlList.get(i));
                try {
                    list.addAll(jsoupUtil.cycle(doc2, string));

                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
            return list;
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        return null;

    }

    @Override
    public String getJobBean(PageBean pageBean) {
        mapper = new ObjectMapper();
        String message = "返回结果小于0!";
        try {
            List<JobBean> list = findConditionMapper.getJobBean(pageBean);
            if (list.size() > 0) {
                message = mapper.writeValueAsString(list);
            }

        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return message;

    }

    @Override
    public String shieldingCompany(String string) {
        int i = findConditionMapper.shieldingCompany(string);
        String message = "屏蔽失败";
        if (i > 0) {
            message = "屏蔽成功";
        }
        return message;
    }

    @Override
    public String totalPages(String string) {
        mapper = new ObjectMapper();
        int i = findConditionMapper.TotalPages(string);
        try {
            return mapper.writeValueAsString(i);
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "数据异常";
    }

    @Override
    // 每五秒执行一次
    @Scheduled(cron = "0 41 16 * * ?")
    public void RegularlyUpdate() {
        List<String> list = findConditionMapper.getSearch();
        for (int i = 0; i < list.size(); i++) {
            List<String> ls = findConditionMapper.getCompany(list.get(i));
            List<JobBean> list2 = getJob(list.get(i));
            List<JobBean> list3 = new ArrayList<JobBean>();

            for (JobBean jobBean : list2) {
                if (ls.contains(jobBean.getCompany())) {
                    list3.add(jobBean);
                }
            }
            list2.removeAll(list3);
            if (list2.size() > 0) {
                int num = findConditionMapper.setJobBean(list2);
                System.out
                        .println(list.get(i)
                                + "分组更新了"
                                + num
                                + "条数据----------------------------------------------------------------------------------------");

            } else {
                System.err.println("数据未更新");
            }
        }

    }
}
